import os
import shutil
from enum import Enum, unique

__author__ = 'wei.zhang'
@unique
class FileTypeEnum(Enum):
    '''
    文件类型定义
    '''
    IMG_FILE_TYPE = ('.img', '.jpg', '.jpeg')
    EXECUTABLE_FILE_TYPE = ('.exe', )
    LOG_FILE_TYPE = ('.log', '.txt')
    MOVIE_FILE_TYPE = ('.mp4', '.mkv')
    COMPRESSED_FILE_TYPE = ('.gz', '.zip', '.rar')
    OTHER = ()


def obtain_filetype_by_filename(filename):
    '''
    根据文件名称获取文件类型
    '''
    print("文件名字是：", filename)
    fileType = str(FileTypeEnum.OTHER)
    if not filename:
        return fileType

    fileNameSuffix = filename[filename.rfind('.'):]

    for name, member in FileTypeEnum.__members__.items():
        curFileTypes = member.value
        if fileNameSuffix in curFileTypes:
            fileType = name
    return fileType


def isSpecial(filename):
    '''
    是否是特殊的文件，用来存放文件的文件夹不用处理
    '''
    isSpecial = False
    for name, member in FileTypeEnum.__members__.items():
        if name == filename:
            print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%", filename)
            isSpecial = True
            break
    return isSpecial

path = 'C:\\Users\\wj\\Downloads'
files = os.listdir(path)

for filename in files:
    if isSpecial(filename):
        continue
    filetype = obtain_filetype_by_filename(filename)

    filePathName = os.path.join(path, filename)
    newFilePath = os.path.join(path, filetype)
    if not os.path.isdir(newFilePath):
        os.makedirs(newFilePath)

    print("原来文件名称%s, 新的文件名称%s" % (filePathName, newFilePath))
    shutil.move(filePathName, newFilePath)



    